package com.geepi.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.geepi.entity.Page;
import com.geepi.entity.User;
import com.geepi.util.DbUtil;


public class UserDaoImpl implements UserDao {

	public List<User> getUserList(String userId, Page page) {
		List<User> userList = new ArrayList<User>();
		ResultSet rs = DbUtil.getList(userId, page);
		System.out.println(userId);
		try {
			// 1:男 0:女
			while (rs.next()) {
				User user = new User();
				user.setUserName(rs.getString("userName"));
				System.out.println("username:" + rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setUserId(rs.getInt("userId"));
				user.setAge(rs.getInt("age"));
				user.setGender(rs.getInt("gender"));// 0-女;1-男
//				user.setHeight(rs.getDouble("height"));
//				user.setBirthday(rs.getTimestamp("birthday"));
				userList.add(user);
			}
			return userList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			DbUtil.closeResource();
		}
	}

	public int updateUser(User user) {
		String sql = "update myuser  set userName='" + user.getUserName()
				+ " ',age=" + user.getAge() + ", gender=" + user.getGender()
				+ ",password='" + user.getPassword() + "' where userId="
				+ user.getUserId() + ";";

		return DbUtil.edit(sql);

	}

	public int delUser(String userId) {

		String sql = "delete from myuser where userId=" + userId;

		return DbUtil.executeUpdate(sql);
	}

	
	public int addUser(User user) {
		String sql = "insert into myuser " 
	+ "(userName,age,gender,password,height,birthday) "
	+" values ( '"+ user.getUserName()+"' , "+user.getAge()+", "
	+ user.getGender()+" ,' "+user.getPassword()+"', "+user.getHeight()
	+", '"+user.getBirthday()+" ' ) ;";
		return DbUtil.executeUpdate(sql);
	}

	
	
	
	public static void main(String[] args) {
		
		
		 
		UserDao ud = new UserDaoImpl();
		User user = new User();
		user.setUserName("吉祥");
		user.setAge(23);
		user.setPassword("222");
		user.setUserId(100);
        user.setBirthday("1997/07/01");
        user.setHeight(1.82);
        user.setGender(1);
		int rt = ud.updateUser(user);
		System.out.println(rt);
         
		/*int m = ud.addUser(user);
		System.out.println(m);*/
	}

}
